package com.administrator.scoreManageSys.service.impl;

import com.administrator.scoreManageSys.entity.system.Permission;
import com.administrator.scoreManageSys.entity.system.Role;
import com.administrator.scoreManageSys.mapper.system.RoleManageMapper;
import com.administrator.scoreManageSys.service.RoleManageService;
import com.administrator.scoreManageSys.service.RoleService;
import com.administrator.scoreManageSys.utils.ResultUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author zsh
 * @date 2022/03/22/ 11:47
 */
@Service
public class RoleManageServiceImpl implements RoleManageService {
    @Resource
    private RoleManageMapper roleManageMapper;
    @Resource
    private ResultUtils resultUtils;
    @Resource
    private RoleService roleService;

    /**
     * 获取角色列表
     *
     * @param pageNum  当前页
     * @param pageSize 页大小
     * @return 角色列表
     */
    @Override
    public JSONObject roleList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> permissions = roleManageMapper.roleList();
        PageInfo<Role> pageInfo = new PageInfo<>(permissions);
        return resultUtils.resultSuccessData(pageInfo);
    }

    /**
     * 添加角色
     *
     * @param name 角色名称
     * @return 操作结果
     */
    @Override
    public JSONObject roleAdd(String name) {
        roleManageMapper.roleAdd(name);
        return resultUtils.resultSuccess();
    }

    /**
     * 修改角色
     *
     * @param id   修改角色id
     * @param name 修改角色名称
     * @return 操作结果
     */
    @Override
    public JSONObject roleUpdate(Long id, String name) {
        roleManageMapper.roleUpdate(id, name);
        return resultUtils.resultSuccess();
    }

    /**
     * 删除角色
     *
     * @param id 删除的角色id
     * @return 操作结果
     */
    @Override
    public JSONObject roleDelete(Long id) {
        roleManageMapper.roleDelete(id);
        roleManageMapper.rolePermissionDelete(id);
        roleService.formatRolePermissionAndMenu();
        return resultUtils.resultSuccess();
    }

    /**
     * 当前角色用户列表
     *
     * @param id       当前角色id
     * @param pageNum  当前页
     * @param pageSize 页大小
     * @return 用户列表
     */
    @Override
    public JSONObject roleUserList(Long id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        JSONArray result = roleManageMapper.roleUserList(id);
        PageInfo<Object> pageInfo = new PageInfo<>(result);
        return resultUtils.resultSuccessData(pageInfo);
    }

    /**
     * 当前角色的权限列表
     *
     * @param id 角色id
     * @return 权限列表
     */
    @Override
    public JSONObject rolePermissionList(Long id) {
        //获取所有权限列表
        List<Permission> result = roleManageMapper.rolePermissionList();
        //获取角色拥有的权限列表
        Set<Integer> permissions = roleManageMapper.roleByPermission(id);

        result.forEach(str -> {
            Long permissionId = str.getId();
            if (!permissions.add(Integer.valueOf(permissionId.toString()))) {
                str.setIsItSelected(1);
            }
            if (str.getChildrenPermission().size() > 0) {
                str.getChildrenPermission().forEach(st -> {
                    Long permission = st.getId();
                    if (!permissions.add(Integer.valueOf(permission.toString()))) {
                        st.setIsItSelected(1);
                    }
                    if (st.getChildrenPermission().size() > 0) {
                        st.getChildrenPermission().forEach(s -> {
                            Long competence = s.getId();
                            if (!permissions.add(Integer.valueOf(competence.toString()))) {
                                s.setIsItSelected(1);
                            }
                        });
                    }
                });
            }
        });
        return resultUtils.resultSuccessData(result);
    }

    /**
     * 添加角色相关的权限
     *
     * @param roleId        角色id
     * @param permissionIds 添加的权限id ,分割
     * @return 操作结果
     */
    @Override
    public JSONObject rolePermissionUpdate(Long roleId, String permissionIds) {
        roleManageMapper.rolePermissionDelete(roleId);
        String[] split = permissionIds.split(",");
        roleManageMapper.rolePermissionUpdate(roleId,split);
        roleService.formatRolePermissionAndMenu();
        return resultUtils.resultSuccess();
    }


}
